<?php

ini_set('display_errors', 1);
ini_set('memory_limit', '512M');
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\trade\Order as mOrder;
use Biz\cache\UserBoughtGoods as cacheUserBoughtGoods;

$start = 0;
$size = 100;
$orderResult = [];
do {
    $orderResult = mOrder::select(['order_no', 'pay_time'], ['AND' => ['status[>]' => mOrder::STATUS_NORMAL, 'pay_way[!]' => '', 'pay_no[!]' => ''], 'LIMIT' => [$start, $size]]);
    $orderList = array_column($orderResult, null, 'order_no');
    $orderNos = array_column($orderResult, 'order_no');
    $goodsList = mOrderGoods::select(['order_no', 'uid', 'goods_id'], ['AND' => ['order_no' => $orderNos]]);
    $list = [];
    foreach ($goodsList as $row) {
        if (isset($orderList[$row['order_no']]['pay_time'])) {
            $list[$row['uid']][$row['goods_id']][] = strtotime($orderList[$row['order_no']]['pay_time']);
        }
    }
    foreach($list as $uid=>$goods){
        foreach($goods as $goodsId=>$row) {
            $time=max($row);
            $userBoughtGoods=[$goodsId=>$time];
            cacheUserBoughtGoods::addBoughtGoods($uid, $userBoughtGoods);
        }
    }

    $end = $start + $size;
    var_dump('complate:'.$start.' to '.$end);
    $start = $end;
} while ($orderResult);

